package com.xuwei.base.reflect;

import java.lang.reflect.Field;
import java.util.Date;

import org.junit.Test;

//反射字段：Field
public class Demo3 {
	//public String name="Bob";
	@Test
	public void test1() throws Exception{
		Class clazz = Person.class;
		Person p = (Person)clazz.newInstance();
		Field f = clazz.getField("name");
		String s = (String)f.get(p);
		System.out.println(s);
		
		//更改name的值
		f.set(p, "Dave");
		System.out.println(p.name);
	}
	@Test//private int age = 30;
	public void test2() throws Exception{
		Class clazz = Person.class;
		Person p = (Person)clazz.newInstance();
		Field f = clazz.getDeclaredField("age");
		f.setAccessible(true);
		int age = (Integer)f.get(p);
		System.out.println(age);
		
		f.set(p, 28);
		age = (Integer)f.get(p);
		System.out.println(age);
	}
	@Test//public static Date time;
	public void test3() throws Exception{
		Class clazz = Person.class;
		Field f = clazz.getField("time");
		f.set(null, new Date());
		System.out.println(Person.time);
	}
}
